#include "reader.h"

Reader::Reader(int dev)
    : running(false)
{
    // register data type for signal and solt support
    qRegisterMetaType<Mat*>("Mat*");

    cap = new VideoCapture(dev);
}

Reader::Reader(string str)
    : running(false)
{
    qRegisterMetaType<Mat*>("Mat*");

    cap = new VideoCapture(str);
}

bool Reader::start(QThread::Priority)
{
    if (cap->isOpened()) {
        running_mutex.lock();
        running = true;
        running_mutex.unlock();
        QThread::start(InheritPriority);
        return true;
    } else {
        running_mutex.lock();
        running = false;
        running_mutex.unlock();
        return false;
    }
}

void Reader::run()
{
    while (true) {
        running_mutex.lock();
        if (running) {
            running_mutex.unlock();

            *cap >> img_in;
            if (!img_in.empty())
                emit updateImage(&img_in);

            msleep(10);
        } else {
            running_mutex.unlock();
            break;
        }
    }
}
